<template>
    <div>
        <el-card shadow="hover">
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="标题">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="接收类型">
                    <el-select v-model="form.receiverType" placeholder="请选择接收类型">
                        <el-option label="所有人" value="1"></el-option>
                        <el-option label="具体用户" value="2"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="用户账号" v-show="form.receiverType == 2">
                    <el-input v-model="form.targetUsername"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input type="textarea" placeholder="请输入内容" v-model="form.content"
                        :autosize="{ minRows: 2, maxRows: 6 }">
                    </el-input>
                </el-form-item>
            </el-form>
            <div class="btn">
                <el-button type="primary" @click="onSubmit">发 送</el-button>
            </div>
        </el-card>
    </div>
</template>

<script>
export default {
    name: "send",
    data() {
        return {
            form: {}
        }
    },
    methods: {
        async onSubmit() {
            let data = {}
            if (this.receiverType == 1) {
                data = {
                    title: this.form.title,
                    content: this.form.content,
                    receiverType: this.form.receiverType,
                    type: 1
                }
            }
            else {
                data = {
                    title: this.form.title,
                    content: this.form.content,
                    receiverType: this.form.receiverType,
                    targetUsername: this.form.targetUsername,
                    type: 1
                }
            }
            let res = await this.$store.dispatch("createNotice", data)
            if (res.success) {
                this.$message({
                    message: "新增成功",
                    type: "success",
                });
                this.form = {}
            } else {
                this.$global.resError(res)
            }
        }
    },
}
</script>

<style scoped>
.btn {
    text-align: center;
}
</style>